package ui.core;

import org.newdawn.slick.Color;
import org.newdawn.slick.Font;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.gui.ComponentListener;
import org.newdawn.slick.gui.GUIContext;

import utils.ResourceManager;

public class TextBox extends AbstractComponent {
	String text;
	String author;
	Image background;
	Font font;
	int x;
	int y;
	int height;
	int width;
	Color color;
	Button button1;
	Button button2;
	boolean b1visible = false;
	boolean b2visible = false;
	String b1text;
	String b2text;
	
	public TextBox(GUIContext container) {
		super(container);
	}
	public TextBox(GUIContext container, int x, int y){
		super(container);
		this.author = "";
		this.x = x;
		this.y = y;
		this.width = 540;
		this.height = 200;
		color = new Color(0,0,0);
		background = ResourceManager.getImage("whiteBackground").copy();
		background.setAlpha((float) 0.7);
		font = ResourceManager.getFont("dejavuNormalFont");
	}
	public TextBox(GUIContext container, int x, int y, int width, int height) {
		this(container, x, y);
		this.height = height;
		this.width = width;		
	}

	public TextBox(GUIContext container, String text, String author, int x,
			int y, int width, int height) {
		this(container, x, y, width, height);
		this.text = text;
		if (!author.isEmpty()) {
			this.author = author + ": ";
		}
	}

	@Override
	public void render(GUIContext container, Graphics g) throws SlickException {
		background.draw(x, y, width, height);
		if(this.author.length()+this.text.length() < 45){
			font.drawString(x + 20, y + 20, this.author + this.text, this.color);
		}else{
			String text1 = text.substring(0, 38);
			String text2 = text.substring(38);
			font.drawString(x + 20, y + 20, author+text1, this.color);
			font.drawString(x + 20, y + 50, text2, this.color);
		}
		if(b1visible){
			button1.render(container, g);
			font.drawString(x+70, y+130, b1text, new Color(0,0,0));
		}
		if(b2visible){
			button2.render(container, g);
			font.drawString(x+320, y+130, b2text, new Color(0,0,0));
		}
	}

	@Override
	public void setLocation(int x, int y) {
		this.x = x;
		this.y = y;
	}

	@Override
	public int getX() {
		return this.x;
	}

	@Override
	public int getY() {
		return this.y;
	}

	@Override
	public int getWidth() {
		return this.width;
	}

	@Override
	public int getHeight() {
		return this.height;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		if (!author.isEmpty()) {
			this.author = author + ": ";
		}
	}	
	public Color getColor() {
		return color;
	}

	public void setColor(Color color) {
		this.color = color;
	}

	public Button getButton1() {
		return button1;
	}

	public void setButton1(GUIContext container, boolean visible, ComponentListener listener, String text) {
		button1 = new Button(container, ResourceManager.getImage("emptyButtonNormalImage"), x+50, y+120);	
		button1.addListener(listener);
		this.b1visible = visible;
		this.b1text = text;
	}

	public Button getButton2() {
		return button2;
	}

	public void setButton2(GUIContext container, boolean visible, ComponentListener listener, String text) {
		button2 = new Button(container, ResourceManager.getImage("emptyButtonNormalImage"), x+300, y+120);
		button2.addListener(listener);
		this.b2visible = visible;
		this.b2text = text;
	}

}
